package com.tudouji.common.utils.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tudouji.common.utils.MD5Utils;
import com.tudouji.common.utils.SecurityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用http发送方法
 *
 * @author ruoyi
 */
public class HttpBusinessUtils
{
    private static final Logger log = LoggerFactory.getLogger(HttpBusinessUtils.class);

    /**
     *
     * @param map  报文
     * @param uri  请求路径
     * @return
     */
    public static Map<String, String> harvestPost(Map<String, String> map, String uri){
        Map<String, String> result=new HashMap<>();
        JSONObject json;
        try {
            StringBuilder build = new StringBuilder();
            for (Map.Entry<String, String> en : map.entrySet()) {
                build.append(en.getKey() + "=" + en.getValue());
            }
            String token = SecurityUtil.encode(MD5Utils.encryString(build.toString()));
            Map<String, Object> param = new HashMap<>();
            param.put("param", map);
            param.put("token", token);
            json = JSONObject.parseObject(JSON.toJSONString(param));
            StringBuffer params = new StringBuffer();
//            Configuration config = new PropertiesConfiguration("systemConfig.properties");
//            params.append(uri);
            params.append("param=");
            params.append(URLEncoder.encode(json.toString(), "UTF-8"));
            String resu= HttpUtils.sendGet(uri, params.toString());
            result.put("result", resu);
            result.put("param", json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
